<template>
  <div class="interview_classify">
    <el-collapse v-model="activeNames"
                 @change="handleChange"
                 class="content">
      <div v-for="(item, index) in classify"
           class="container"
           :key="item.category">
        <el-collapse-item :title="item.category"
                          :name="item.category"
                          class="content_item">
          <interview-item v-for="storage in item.storage"
                          :item="storage"
                          :category="item.category"></interview-item>
        </el-collapse-item>
        <el-button type="text"
                   @click="delClassify(index)"
                   class="del">
          <div class="el-icon-delete"></div>
        </el-button>
      </div>
    </el-collapse>
      <el-button type="text" @click="addClassify" class="add"></el-button>
  </div>
</template>

<script>
  import InterviewItem from "./InterviewItem";

  import {addClassify, delClassify} from "network/collect";

  export default {
    name: "InterviewClassify",
    components: {
      InterviewItem
    },
    props: {
      classify: {
        type: Array,
        default() {
          return []
        }
      }
    },
    data() {
      return {
        activeNames: ['1'],
      };
    },
    methods: {
      handleChange(val) {
        // console.log(val);
      },
      addClassify() {
        this.$prompt('请输入要添加的分类', '添加新的分类', {
          confirmButtonText: '添加',
          cancelButtonText: '取消',
          inputPattern: /./,
          inputErrorMessage: '请输入想要设置的分类'
        }).then(({value}) => {
          const obj = {
            userId: this.$store.state.userInfo.id,
            type: "articles",
            category: value
          }
          addClassify(obj).then(res => {
            console.log(res)
            if(res.data.code) {
              this.$toast.show('成功创建分类', 'success', 1000)
              this.$bus.$emit('collectRefresh')
            }
          })
        }).catch(() => {});
      },
      delClassify(index) {
        this.$confirm('删除后该分类的内容将不存在，是否删除', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const obj = {
            userId: this.$store.state.userInfo.id,
            type: "articles",
            category: this.classify[index].category
          }
          delClassify(obj).then(res => {
            if(res.data.code) {
              this.$toast.show('成功删除分类', 'success', 1000)
              this.$bus.$emit('collectRefresh')
            }
          })
        }).catch(() => {});
      },
    }
  }
</script>

<style scoped>
  .content {
    width: 740px;
    margin: 0 auto;
    border-top: transparent;
    border-bottom: transparent;
  }

  .container {
    position: relative;
  }

  .content_item {
    padding-right: 5px;
    text-indent: 10px;
  }

  .container .del {
    position: absolute;
    top: 14px;
    right: -10px;
    padding: 0;
    color: #ebeef1;
    cursor: pointer;
  }

  .container .el-icon-delete:hover {
    color: #002766;
  }

  .add {
    position: relative;
    width: 120px;
    height: 35px;
    margin: 15px 25px;
    background-color: #3f579e;
    border: 1px solid #eeeeee;
  }

  .add::before {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 25px;
    margin-left: -13px;
    margin-top: -1px;
    border-top: 2px solid #eee;
    content: '';
  }

  .add::after {
    position: absolute;
    left: 50%;
    top: 50%;
    height: 26px;
    margin-left: -1px;
    margin-top: -13px;
    border-left: 2px solid #eee;
    content: '';
  }

</style>
